﻿using System;
using System.Globalization;
using System.Web.UI;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.HContact
{
    public partial class EditContact02 : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TextBoxFirstName.Focus();
                BindDropDownListContactType();
                BindDropDownListBestContactMethod();
                BindFields();
                var filter = new HContactFilter();
                ViewState["filter"] = filter;
            }
        }

        /// <summary>
        /// Binds the type of the drop down list contact.
        /// </summary>
        protected void BindDropDownListContactType()
        {
            DropDownListContactType.DataSource = HungContactBO.SelectAllContactTypes();
            DropDownListContactType.DataBind();
        }

        /// <summary>
        /// Binds the drop down list best contact method.
        /// </summary>
        protected void BindDropDownListBestContactMethod()
        {
            DropDownListBestContactMethod.DataSource = HungContactBO.SelectAllBestMethodContact();
            DropDownListBestContactMethod.DataBind();
        }

        /// <summary>
        /// Binds the fields.
        /// </summary>
        protected void BindFields()
        {
            int contactId = 0;
            if (!int.TryParse(Request.Params["contactId"], out contactId))
            {
                return;
            }

            Contact contact = HungContactBO.SelectContactById(contactId);
            if (contact == null)
            {
                return;
            }

            HiddenFieldContactId.Value = contact.ContactId.ToString(CultureInfo.CurrentCulture);
            ButtonSave.Enabled = true;

            //bind contact to fields
            TextBoxFirstName.Text = contact.FirstName;
            TextBoxSurname.Text = contact.Surname;
            HiddenFieldManagerId.Value = contact.ManageId.ToString();
            TextBoxKnowAs.Text = contact.KnownAs;
            TextBoxOfficePhone.Text = contact.OfficePhone;
            TextBoxMobilePhone.Text = contact.MobilePhone;
            TextBoxHomePhone.Text = contact.HomePhone;
            TextBoxEmailAddress.Text = contact.Email;
            if (contact.ManageId != null)
                TextBoxManagerId.Text = HungContactBO.SelectContactById((int)contact.ManageId).FirstName + ' ' +
                                        HungContactBO.SelectContactById((int)contact.ManageId).Surname;
            DropDownListContactType.SelectedValue = contact.ContactTypeId.ToString(CultureInfo.CurrentCulture);
            if (contact.BestContactMethodId != null)
                DropDownListBestContactMethod.SelectedValue =
                    contact.BestContactMethodId.Value.ToString(CultureInfo.InvariantCulture);
            TextBoxRole.Text = contact.JobRole;
            TextBoxWorkBase.Text = contact.WorkBase;
            TextBoxJobTitle.Text = contact.JobTitle;
            CheckBoxIsActive.Checked = contact.IsActive;
        }

        /// <summary>
        /// Gets the contact from fields in page
        /// </summary>
        /// <returns></returns>
        protected Contact SelectContact()
        {
            int contactId = int.Parse(HiddenFieldContactId.Value, CultureInfo.CurrentCulture);
            int contactTypeId = Convert.ToInt32(DropDownListContactType.SelectedValue, CultureInfo.CurrentCulture);
            int bestContactMethodId = Convert.ToInt32(DropDownListBestContactMethod.SelectedValue,
                                                      CultureInfo.CurrentCulture);
            var contact = new Contact();

            contact.ContactId = contactId;
            contact.FirstName = TextBoxFirstName.Text;
            contact.Surname = TextBoxSurname.Text;
            contact.KnownAs = TextBoxKnowAs.Text;
            contact.OfficePhone = TextBoxOfficePhone.Text;
            contact.MobilePhone = TextBoxMobilePhone.Text;
            contact.HomePhone = TextBoxHomePhone.Text;
            contact.Email = TextBoxEmailAddress.Text;
            contact.ManageId = string.IsNullOrEmpty(HiddenFieldManagerId.Value)
                                   ? (int?)null
                                   : Convert.ToInt32(HiddenFieldManagerId.Value, CultureInfo.CurrentCulture);
            contact.ContactTypeId = contactTypeId;
            contact.BestContactMethodId = bestContactMethodId;
            contact.JobRole = TextBoxRole.Text;
            contact.WorkBase = TextBoxWorkBase.Text;
            contact.JobTitle = TextBoxJobTitle.Text;
            contact.IsActive = CheckBoxIsActive.Checked;

            return contact;
        }

        /// <summary>
        /// Handles the Click event of the ButtonBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonBackClick(object sender, EventArgs e)
        {
            Response.Redirect("ContactList.aspx");
        }

        /// <summary>
        /// Handles the Click event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            Contact contact = SelectContact();
            bool result = HungContactBO.UpdateContact(contact);
            if (result)
            {
                Response.Redirect("ContactList.aspx");
            }
        }

        /// <summary>
        /// Links the button lookup click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkButtonLookupClick(object sender, EventArgs e)
        {
            ContactLookup.Popup();
        }
    }
}